<?php
  
  //Main 
  $keys = array("country", "filter","key");
  $value = array(); 
  foreach( $keys as $key ) {
    $value[$key] = $_GET[$key]; 
  }

  if($value['country'] && $value['filter']){
    if($value['filter']=="province"){
      //Get province
      include("Province.class.php");
      $vote = new Province();

      //var_dump($value);
      //exit;
      if($value['key'] && strlen($value['key'])>0){
        $vote->searchProvince($value);
      }else{
        $vote->getProvince($value);
      }
      unset($vote);
    }else if($value['filter']=="district"){
      //Get District
      include("District.class.php");
      $vote = new District();

      if($value['key'] && strlen($value['key'])>0){
        $vote->searchDistrict($value);
      }else{
        $value['pro'] = $_GET['pro']; 
        $vote->getDistrict($value);
      }
      unset($vote);
    }else if($value['filter']=="tambon"){
      //Get Tambon
      include("Tambon.class.php");
      $vote = new Tambon();

      if($value['key'] && strlen($value['key'])>0){
        $vote->searchTambon($value);
      }else{

        $value['pro'] = $_GET['pro']; 
        $value['dis'] = $_GET['dis']; 
        $vote->getTambon($value);
      }
      unset($vote);
    }else if($value['filter']=="moo"){
      //Get Moo
      $value['pro'] = $_GET['pro']; 
      $value['dis'] = $_GET['dis']; 
      $value['tam'] = $_GET['tam']; 
      include("Moo.class.php");
      $vote = new Moo();
      $vote->getMoo($value);
      unset($vote);
    }else if($value['filter']=="house_no"){
      //Get HouseNo
      include("HouseNo.class.php");
      $vote = new HouseNo();
      $value['pro'] = $_GET['pro']; 
      $value['dis'] = $_GET['dis']; 
      $value['tam'] = $_GET['tam']; 
      $value['moo'] = $_GET['moo']; 

      $vote->getHouseNo($value);
      unset($vote);
    }else if($value['filter']=="person"){
      //Get Person
      include("Person.class.php");
      $vote = new Person();

      $value['first'] = $_GET['first']; 
      $value['last'] = $_GET['last']; 

      if($value['first'] && strlen(trim($value['first']))>0 && $value['last'] && strlen(trim($value['last']))>0 ){
        $vote->searchPerson($value);
      }else if($value['first'] && strlen(trim($value['first']))>0 ){
        $vote->searchFirstName($value);
      }else if($value['last'] && strlen(trim($value['last']))>0 ){
        $vote->searchLastName($value);
      }else{
        $value['pro'] = $_GET['pro']; 
        $value['dis'] = $_GET['dis']; 
        $value['tam'] = $_GET['tam']; 
        $value['moo'] = $_GET['moo']; 
        $value['hou'] = $_GET['hou']; 
        $vote->getPerson($value);
      }
      unset($vote);
    }else {
      $response['errorCode'] = 6;
      $response['errorString'] = "Wrong parameter filter.";
      setErrorResponse($response);
    }
  }else{
      $response['errorCode'] = 7;
      $response['errorString'] = "Parameter incomplete.";
      setErrorResponse($response);
  }

  function setErrorResponse($response){
        //Gen food
        $xml = new DOMDocument('1.0', 'UTF-8');
        $xml->formatOutput = true;

        $xml_node_response = $xml->createElement("response");
        $xml->appendChild( $xml_node_response );
        $xml_node_errorcode = $xml->createElement( "errorcode", $response['errorCode']);
        $xml_node_response->appendChild( $xml_node_errorcode );
        $xml_node_errorstring = $xml->createElement( "errorstring", $response['errorString']);
        $xml_node_response->appendChild( $xml_node_errorstring );

        echo $xml->saveXML();
  }
?>

